﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;

namespace FFSupplier.Common.TKCommon
{
    public class Dynamics365WebApiHelper
    {
        ICrmConfiguration _configuration;

        public Dynamics365WebApiHelper(ICrmConfiguration configuration)
        {
            _configuration = configuration;
        }

        public HttpClient GetHttpClient()
        {
            HttpMessageHandler messageHandler;
            NetworkCredential credentials = new NetworkCredential(_configuration.GetUserName(), _configuration.getPassword(), _configuration.getDomain());
            messageHandler = new HttpClientHandler() { Credentials = credentials };



            Guid userid = Guid.Empty;

            HttpClient httpClient = new HttpClient(messageHandler);

            //Specify the Web API address of the service and the period of time each request 
            // has to execute.
            httpClient.BaseAddress = new Uri(_configuration.orgUrl());
            httpClient.Timeout = new TimeSpan(0, 2, 0);  //2 minutes

            //Send the WhoAmI request to the Web API using a GET request. 

            return httpClient;

        }

        public HttpClient GetHttpClientWithCredential()
        {
            HttpMessageHandler messageHandler;
            NetworkCredential credentials = new NetworkCredential(_configuration.GetUserName(), _configuration.getPassword(), _configuration.getDomain());
            messageHandler = new HttpClientHandler() { Credentials = credentials };



            Guid userid = Guid.Empty;

            HttpClient httpClient = new HttpClient(messageHandler);

            //Specify the Web API address of the service and the period of time each request 
            // has to execute.
            //httpClient.BaseAddress = new Uri(_configuration.orgUrl());
            httpClient.Timeout = new TimeSpan(0, 2, 0);  //2 minutes
            httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
            httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
            //  httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
            // httpClient.DefaultRequestHeaders.Add("Content-Type", "application/json; charset=utf-8");
            httpClient.DefaultRequestHeaders.Add("Prefer", "odata.include-annotations=\"*\"");
            //Send the WhoAmI request to the Web API using a GET request. 

            return httpClient;

        }
    }
}